<?php
/**
* @file $Id: Portal.php 588 2007-07-07 01:06:59Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

$last_login = $_SESSION['LAST_LOGIN'];
DrawHeader(DrawPNG('icons/School_Setup.png').'<IMG SRC=assets/pixel_trans.gif height=1 width=4><font size=+1>'._('Portal').'</font><BR> &nbsp; &nbsp; &nbsp; '._('Welcome, ').User('NAME'),'<BR> &nbsp; '._('Last login').': '.($last_login?$last_login:''));

if(!$_SESSION['UserSyear'])
	$_SESSION['UserSyear'] = $DefaultSyear;

if(User('PROFILE_ID'))
	$notes_RET = DBGet(DBQuery("SELECT ID,TITLE,CONTENT,PUBLISHED_DATE,PUBLISHED_USER,COALESCE(START_DATE,PUBLISHED_DATE) AS START_DATE FROM PORTAL_NOTES WHERE ('".DBDate()."' BETWEEN START_DATE AND END_DATE OR START_DATE IS NULL OR (END_DATE IS NULL AND '".DBDate()."'>=START_DATE)) AND PUBLISHED_PROFILES LIKE '%,".User('PROFILE_ID').",%' ORDER BY SORT_ORDER,END_DATE DESC"));
else
	$notes_RET = DBGet(DBQuery("SELECT ID,TITLE,CONTENT,PUBLISHED_DATE,PUBLISHED_USER,COALESCE(START_DATE,PUBLISHED_DATE) AS START_DATE FROM PORTAL_NOTES WHERE ('".DBDate()."' BETWEEN START_DATE AND END_DATE OR START_DATE IS NULL OR (END_DATE IS NULL AND '".DBDate()."'>=START_DATE)) AND PUBLISHED_PROFILES LIKE '%,0,%' ORDER BY SORT_ORDER,END_DATE DESC"));
	
$events_RET = DBGet(DBQuery("SELECT ID,SCHOOL_DATE,TITLE,DESCRIPTION FROM CALENDAR_EVENTS WHERE SCHOOL_DATE>='".DBDate()."' AND SYEAR='".UserSyear()."' ".(User('SCHOOLS')?"AND SCHOOL_ID IN ('".str_replace(',',"','",substr(User('SCHOOLS'),1,-1))."')":'')." ORDER BY SCHOOL_DATE ASC LIMIT 7"),$functions);

if(User('PROFILE_ID'))
	$where = "PROFILE_ID='".User('PROFILE_ID')."'";
else
	$where = "STAFF_ID='".User('STAFF_ID')."'";
//$programs_RET = DBGet(DBQuery("SELECT MODNAME FROM PORTAL_PROGRAMS WHERE ".$where));

if(User('SCHOOLS'))
	$schools_extra = ' AND SCHOOL_ID IN ('.substr(str_replace(",","','",User('SCHOOLS')),2,-2).') ';
else
	$schools_extra = '';

$message = '';
$today = DBDate();

if(AllowUse('Students/AddDrop.php') && User('LAST_LOGIN'))
{
	$last_login['d'] = substr(User('LAST_LOGIN'),8);
	$last_login['m'] = MonthNWSwitch(substr(User('LAST_LOGIN'),5,2),'tochar');
	$last_login['y'] = substr(User('LAST_LOGIN'),2,2);
	$added_RET = DBGet(DBQuery("SELECT COUNT(*) AS COUNT FROM STUDENT_ENROLLMENT WHERE START_DATE BETWEEN '".User('LAST_LOGIN')."' AND '".DBDate()."'".$schools_extra));
	if($added_RET[1]['COUNT']>0)
	{
		$message .= '<A HREF=Modules.php?modname=Students/AddDrop.php&day_start='.$last_login['d'].'&month_start='.$last_login['m'].'&year_start='.$last_login['y'].'>'.DrawPNG('icons/Students.png','border=0');
		if($added_RET[1]['COUNT']==1)
			$message .= $added_RET[1]['COUNT'].' new student';
		else
			$message .= $added_RET[1]['COUNT'].' new students';
		$message .= '</A><BR>';
	}
	$dropped_RET = DBGet(DBQuery("SELECT COUNT(*) AS COUNT FROM STUDENT_ENROLLMENT WHERE END_DATE BETWEEN '".User('LAST_LOGIN')."' AND '".DBDate()."'".$schools_extra));
	if($dropped_RET[1]['COUNT']>0)
	{
		$message .= '<A HREF=Modules.php?modname=Students/AddDrop.php&day_start='.$last_login['d'].'&month_start='.$last_login['m'].'&year_start='.$last_login['y'].'>'.DrawPNG('icons/Students.png','border=0');
		if($dropped_RET[1]['COUNT']==1)
			$message .= $dropped_RET[1]['COUNT'].' new student';
		else
			$message .= $dropped_RET[1]['COUNT'].' new students';
		$message .= '</A><BR>';
	}
}

if(AllowUse('Attendance/TeacherCompletion.php') && GetCurrentMP('QTR',$today,false))
{
	$day = date('D');
	switch($day)
	{
		case 'Sun':
			$day = 'U';
		break;
		case 'Thu':
			$day = 'H';
		break;
		default:
			$day = substr($day,0,1);
		break;
	}
	
	$QI = DBQuery("SELECT PERIOD_ID,TITLE FROM SCHOOL_PERIODS WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' ORDER BY SORT_ORDER ");
	$periods_RET = DBGet($QI);
		
	$today = DBDate();
	$yesterday_time = (time() - 86400);
	$yesterday = strtoupper(date('d-M-y',$yesterday_time));
	$yesterday_day = date('D',$yesterday_time);
	switch($yesterday_day)
	{
		case 'Sun':
			$yesterday_day = 'U';
		break;
		case 'Thu':
			$yesterday_day = 'H';
		break;
		default:
			$yesterday_day = substr($yesterday_day,0,1);
		break;
	}

	$att_RET = DBGet(DBQuery("SELECT COUNT(DISTINCT STAFF_ID) AS COUNT
			FROM USERS s,COURSE_PERIODS cp,SCHOOL_PERIODS sp,ATTENDANCE_CALENDAR ac 
			WHERE 
				ac.CALENDAR_ID=cp.CALENDAR_ID AND ac.SCHOOL_DATE='".$today."'
				AND sp.PERIOD_ID = cp.PERIOD_ID
				AND cp.TEACHER_ID=s.STAFF_ID AND cp.MARKING_PERIOD_ID IN (".GetAllMP('QTR',GetCurrentMP('QTR',$today,false)).") 
				AND cp.SYEAR='".UserSyear()."' AND cp.SCHOOL_ID='".UserSchool()."' AND s.PROFILE='teacher'
				AND cp.DOES_ATTENDANCE='Y' AND cp.DAYS LIKE '%".$day."%'
				AND NOT EXISTS (SELECT '' FROM ATTENDANCE_COMPLETED ac WHERE ac.STAFF_ID=cp.TEACHER_ID AND ac.SCHOOL_DATE='".$today."' AND ac.PERIOD_ID=sp.PERIOD_ID)
			"));
	if($att_RET[1]['COUNT']>0)
	{
		$message .= '<A HREF=Modules.php?modname=Attendance/TeacherCompletion.php&day_date='.date('d').'&month_date='.strtoupper(date('M')).'&year_date='.date('y').'>'.DrawPNG('icons/Users.png','border=0');
		if($att_RET[1]['COUNT']==1)
			$message .= $att_RET[1]['COUNT']._(' teacher hasn\'t taken attendance today');
		else
			$message .= $att_RET[1]['COUNT']._(' teachers haven\'t taken attendance today');
		$message .= '</A><BR>';	
	}
	$att_RET = DBGet(DBQuery("SELECT COUNT(DISTINCT STAFF_ID) AS COUNT
			FROM USERS s,COURSE_PERIODS cp,SCHOOL_PERIODS sp,ATTENDANCE_CALENDAR ac
			WHERE 
				ac.CALENDAR_ID=cp.CALENDAR_ID AND ac.SCHOOL_DATE='".$yesterday."'
				AND sp.PERIOD_ID = cp.PERIOD_ID
				AND cp.TEACHER_ID=s.STAFF_ID AND cp.MARKING_PERIOD_ID IN (".GetAllMP('QTR',GetCurrentMP('QTR',$yesterday,false)).") 
				AND cp.SYEAR='".UserSyear()."' AND cp.SCHOOL_ID='".UserSchool()."' AND s.PROFILE='teacher'
				AND cp.DOES_ATTENDANCE='Y' AND cp.DAYS LIKE '%".$yesterday_day."%'
				AND NOT EXISTS (SELECT '' FROM ATTENDANCE_COMPLETED ac WHERE ac.STAFF_ID=cp.TEACHER_ID AND ac.SCHOOL_DATE='".$yesterday."' AND ac.PERIOD_ID=sp.PERIOD_ID)
			"));
	if($att_RET[1]['COUNT']>0)
	{
		$message .= '<A HREF=Modules.php?modname=Attendance/TeacherCompletion.php&day_date='.date('d',$yesterday_time).'&month_date='.strtoupper(date('M',$yesterday_time)).'&year_date='.date('y',$yesterday_time).'>'.DrawPNG('icons/Users.png','border=0');
		if($att_RET[1]['COUNT']==1)
			$message .= $att_RET[1]['COUNT']._(' teacher didn\'t take attendance yesterday');
		else
			$message .= $att_RET[1]['COUNT']._(' teachers didn\'t take attendance yesterday');
		$message .= '</A><BR>';	
	}
}

if(AllowUse('Discipline/Referrals.php') && User('LAST_LOGIN'))
{
	$extra = array();
	$extra['SELECT_ONLY'] = 'count(*) AS COUNT';
	$extra['FROM'] = ',DISCIPLINE_REFERRALS dr ';
	$extra['WHERE'] = ' AND dr.STUDENT_ID=ssm.STUDENT_ID AND dr.SYEAR=ssm.SYEAR AND dr.SCHOOL_ID=ssm.SCHOOL_ID AND dr.ENTRY_DATE BETWEEN '."'".User('LAST_LOGIN')."' AND '".$today."'";
	
	$disc_RET = GetStuList($extra);

	if($disc_RET[1]['COUNT']>0)
	{
		$message .= '<A HREF=Modules.php?modname=Discipline/Referrals.php&search_modfunc=list&discipline_entry_begin='.User('LAST_LOGIN').'&discipline_entry_end='.$today.'>'.DrawPNG('icons/Discipline.png','border=0');
		if($disc_RET[1]['COUNT']==1)
			$message .= $disc_RET[1]['COUNT']._(' new referral');
		else
			$message .= $disc_RET[1]['COUNT']._(' new referrals');
		$message .= '</A><BR>';	
	}
}

if(AllowUse('Attendance/Administration.php') && User('LAST_LOGIN'))
{
	$yesterday_time = (time() - 86400);
	$yesterday = date('d-M-y',$yesterday_time);

	$extra = array();
	$extra['SELECT_ONLY'] = 'sum((STATE_VALUE-1))*-1 AS COUNT';
	$extra['FROM'] = ',ATTENDANCE_DAY ad ';
	$extra['WHERE'] = " AND ad.STUDENT_ID=ssm.STUDENT_ID AND ad.SCHOOL_DATE='".$today."'";
	$att_RET = GetStuList($extra);
	$abs_count = str_replace('.0','',$att_RET[1]['COUNT']);

	if($abs_count>0)
	{
		$message .= '<A HREF=Modules.php?modname=Attendance/Administration.php>'.DrawPNG('icons/Attendance.png','border=0');
		if($abs_count==1)
			$message .= $abs_count._(' student absent today');
		else
			$message .= $abs_count._(' students absent today');
		$message .= '</A><BR>';	
	}

	$extra = array();
	$extra['DATE'] = $yesterday;
	$extra['SELECT_ONLY'] = 'sum((STATE_VALUE-1))*-1 AS COUNT';
	$extra['FROM'] = ',ATTENDANCE_DAY ad ';
	$extra['WHERE'] = " AND ad.STUDENT_ID=ssm.STUDENT_ID AND ad.SCHOOL_DATE='".$yesterday."'";
	$att_RET = GetStuList($extra);
	$abs_count = str_replace('.0','',$att_RET[1]['COUNT']);

	if($abs_count>0)
	{
		$message .= '<A HREF=Modules.php?modname=Attendance/Administration.php&day_date='.date('d',$yesterday_time).'&month_date='.strtoupper(date('M',$yesterday_time)).'&year_date='.date('y',$yesterday_time).'>'.DrawPNG('icons/Attendance.png','border=0');
		if($abs_count==1)
			$message .= $abs_count._(' student absent yesterday');
		else
			$message .= $abs_count._(' students absent yesterday');
		$message .= '</A><BR>';	
	}
}

$blocks_count = 0;
if($message)
	$blocks_count++;
if(count($notes_RET))
	$blocks_count++;
if(count($events_RET))
	$blocks_count++;
//if(count($programs_RET))
//	$blocks_count++;


echo '<BR>';
if($blocks_count)
{
	$width = floor(100 / $blocks_count);
	echo '<TABLE border=0 cellspacing=0 cellpadding=0 class=innerLO_field width=100%><TR>';
}

if($message)
{
	echo '<TD width='.$width.'% valign=top height=100%>';
	DrawBlock(_('Alerts'),$message);
	echo '</TD>';
}


if(count($notes_RET))
{
	echo '<TD width='.$width.'% valign=top>';
	$most_recent_note = 0;
	$content = '';
	foreach($notes_RET as $note)
	{
		$content .= '<TABLE class=BoxContents><TR><TD valign=middle>';
		if($note['ID']<=User('LAST_NOTE_SEEN'))
		{
			if($_FOCUS['direction']=='RTL')
				$content .= '<IMG SRC=assets/arrow_left.gif id=note'.$note['ID'].'_arrow>';
			else
				$content .= '<IMG SRC=assets/arrow_right.gif id=note'.$note['ID'].'_arrow>';
		}
		else
			$content .= '<IMG SRC=assets/warning_button.gif width=15 id=arrownote'.$note['ID'].'>';
		$content .= "</TD><TD><A HREF=# onclick='switchMenu(\"note$note[ID]\");'><B>".$note['TITLE']."</B></A>  <font color=gray>".' ('.ProperDate($note['START_DATE']).')</font></TD></TR><TR><TD></TD><TD>';
		if($note['ID']<=User('LAST_NOTE_SEEN'))
			$display = 'none';
		else
			$display = 'block';

		$content .= "<DIV id=note$note[ID] style='display:".$display.";'>".nl2br($note['CONTENT']).'</DIV>';
		$content .= '</TD></TR></TABLE>';
		if($note['ID']>$most_recent_note)
			$most_recent_note = $note['ID'];
	}
	DrawBlock(_('Messages'),$content);
	echo '</TD>';
}


if(count($events_RET))
{
	echo '<TD width='.$width.'% valign=top>';
	$content = '';
	foreach($events_RET as $event)
	{
		$content .= '<TABLE class=BoxContents><TR><TD valign=middle>';
		if($_FOCUS['direction']=='RTL')
			$content .= '<IMG SRC=assets/arrow_left.gif id=calendar'.$event['ID'].'_arrow>';
		else
			$content .= '<IMG SRC=assets/arrow_right.gif id=calendar'.$event['ID'].'_arrow>';

		if(ProperDate($event['SCHOOL_DATE'])==ProperDate(DBDate()))
			$content .= '</TD><TD><A HREF=# onclick=\'switchMenu("calendar'.$event['ID'].'");\'><font color=red style="text-decoration:none;"><B>'.ProperDate($event['SCHOOL_DATE']).'</B></font>: <B>'.$event['TITLE'].'</B></A>';
		else
			$content .= '</TD><TD><A HREF=# onclick=\'switchMenu("calendar'.$event['ID'].'");\'><font color=gray style="text-decoration:none;">'.ProperDate($event['SCHOOL_DATE']).'</font>: <B>'.$event['TITLE'].'</B></A>';
		$content .= "<DIV id=calendar$event[ID] style='display:none;'>".$event['DESCRIPTION'].'</DIV>';

		$content .= '</TD></TR></TABLE>';
	}
	DrawBlock(_('Upcoming Events'),$content,'width=100% height=100%');
	echo '</TD>';
}

if($blocks_count)
	echo '</TR></TABLE>';

echo '<BR><BR>';
if(User('PROFILE')=='admin')
{
	echo '<FORM action=Modules.php?modname=Students/Search.php&search_modfunc=list method=POST>';
	DrawBlock(_('Find a Student'),'<INPUT type=text name=smart_name><INPUT type=submit value="'._('Search').'">','width=100%');	
	echo '</FORM>';
	echo '<BR>';
}

/*
if(count($programs_RET))
{
	$i = 0;
	$content = '<TABLE class=BoxContents><TR>';
	foreach($programs_RET as $program)
	{
		$i++;
		$content .= '<TD valign=middle>';
		foreach($_FOCUS['Menu'] as $modcat=>$programs)
		{
			if($programs[$program['MODNAME']])
				break;
		}
		$content .= '<IMG SRC=assets/icons/'.$modcat.'.gif height=15>';
		$content .= '</TD><TD><A HREF=Modules.php?modname='.$program['MODNAME'].'><B>'.ProgramTitle($program['MODNAME']).'</B></A>';
		$content .= '</TD><TD width=5> &nbsp; </TD>';
		
		if($i%5==0)
			$content .= '</TR><TR>';
	}
	$content .= '</TR></TABLE>';
	
	DrawBlock(_('Featured Programs'),$content,'width=100%');
	echo '<BR>';
}
*/
DBQuery("UPDATE USERS SET LAST_NOTE_SEEN='".$most_recent_note."' WHERE STAFF_ID='".User('STAFF_ID')."'");
if(!$programs_RET)
{
	require($staticpath.'Menu.php');
	$content = '<TABLE><TR>';
	$i = 0;
	foreach($_FOCUS['Menu'] as $modcat=>$programs)
	{
		if(count($_FOCUS['Menu'][$modcat]))
		{
			$keys = array_keys($_FOCUS['Menu'][$modcat]);
			$menu = false;
			foreach($keys as $key_index=>$file)
			{
				if(!is_numeric($file))
					$menu = true;
			}
			if(!$menu)
				continue;
	
			if($i%3==0)
				$content .= '</TR><TR><TD colspan=3 height=10> &nbsp; </TD></TR><TR>';
			$i++;
			$content .= '<TD valign=top>';
	
			switch($modcat)
			{
				case 'School_Setup':
					$localized_modcat = _('School Setup');
				break;
				
				case 'Students':
					$localized_modcat = _('Students');
				break;
				
				case 'Users':
					$localized_modcat = _('Users');
				break;
				
				case 'Grades':
					$localized_modcat = _('Grades');
				break;
	
				case 'School_Information':
					$localized_modcat = _('School Information');
				break;
	
				case 'My_Information':
					$localized_modcat = _('My Information');
				break;
	
				case 'My_Child':
					$localized_modcat = _('My Child');
				break;
				
				case 'Classes_&_Grades':
					$localized_modcat = _('Classes & Grades');
				break;
				
				case 'Attendance':
					$localized_modcat = _('Attendance');
				break;
	
				case 'Eligibility':
					$localized_modcat = _('Eligibility');
				break;
				
				case 'Discipline':
					$localized_modcat = _('Discipline');
				break;
				
				case 'Billing':
					$localized_modcat = _('Billing');
				break;
	
				case 'Reports':
					$localized_modcat = _('Reports');
				break;
				
				default:
					$localized_modcat = str_replace('_',' ',$modcat);
				break;
			}
			$content .= DrawPNG('icons/'.$modcat.'.png','border=0')."<font face='Verdana, Arial, sans-serif'><B>".$localized_modcat."</B></font><BR>";
			$content .= "<TABLE>";
			//foreach($_FOCUS['Menu'][$modcat] as $file=>$title)
			foreach($keys as $key_index=>$file)
			{
				$title = $_FOCUS['Menu'][$modcat][$file];
				if(substr($file,0,7)=='http://')
					$content .= "<TR><TD width=20></TD><TD class=BoxContents style='background-color:transparent'><b>&rsaquo;</b> <A HREF=$file target=body onclick='javascript:parent.help.location=\"Bottom.php?modname=$file\"'><font color=blue>$title</font></A></TD></TR>";					
				elseif(!is_numeric($file))
					$content .= "<TR><TD width=20></TD><TD class=BoxContents style='background-color:transparent'><b>&rsaquo;</b> <A HREF=Modules.php?modname=$file target=body onclick='javascript:parent.help.location=\"Bottom.php?modname=$file\"'><font color=blue>$title</font></A></TD></TR>";
				elseif($keys[$key_index+1] && !is_numeric($keys[$key_index+1]))
					$content .= '<TR><TD colspan=2 height=3></TD></TR><TR><TD colspan=2 class=BoxContents style="background-color:transparent"> &nbsp; <b>'.$title.'</b></TD></TR>';
			}
			$content .= "</TABLE>";
			$content .= '</TD>';
		}
	}
	$content .= '</TR></TABLE>';
	
//	DrawBlock(_('All Programs'),$content,'width=100%');
}
?>